package com.gupao.javap6.pattern.proxy;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 模拟数据库
 *
 * @author withinfly(45度C)
 * @date 2020/3/1 23:50
 */
public final class DB<T> {
    private Map<Serializable, T> dbMaster = new HashMap<>();
    private Map<Serializable, T> dbSlave = new HashMap<>();

    private static DB db = new DB<>();

    public static <T> int put(Serializable id, T object) {
        db.dbMaster.put(id, object);
        db.dbSlave.put(id, object);
        return 1;
    }

    public static int remove(Serializable id) {
        db.dbMaster.remove(id);
        db.dbSlave.remove(id);
        return 1;
    }

    public static <T> T get(Serializable id) {
        T object = (T)db.dbSlave.get(id);
        return object;
    }

    public static <T> List<T> values() {
        List<T> collect = (List<T>)db.dbSlave.values().stream().collect(Collectors.toList());
        return collect;
    }

}
